<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 属性值数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\service\AttributeValueService;
use common\validate\AttributeValue;
use support\ErrorCode;
use think\Request;

class AttributeValueController extends VerifyController
{

    /**
     * @var AttributeValueService
     */
    private $attributeValueService;

    public function __construct()
    {
        parent::__construct();
        $this->attributeValueService = new AttributeValueService();
    }

    /**
     * 批量创建属性值
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function saveBatch(Request $request)
    {
        $param = $request->param();
        $validator = new AttributeValue();
        $validator->scene("SaveBatchItem");
        foreach ($param['data'] as $attributeValue) {
            if (!$validator->check($attributeValue)) {
                throw_strack_exception($validator->getError(),  ErrorCode::ATTRIBUTE_BATCH_SAVE_PARAM);
            }
        }
        $res = $this->attributeValueService->saveBatch($param['data']);
        return json(success_response('', $res));
    }
}
